#include <QMainWindow>
#include <QMenu>
#include <QAction>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QIcon>
#include <QVector>
#include <QMap>
#include <QDebug>
// 假设 ui 对应的头文件，根据实际项目调整
#include "updateui.h"
#include "ui_MainWindow.h" 

UpdateUI::UpdateUI(QWidget* parent)
    : QWidget(parent)
    , ui(MainWindow::mywidget->ui)
{
    //!这是构造函数，所有初始化放到这里面
     // 星期和日期
    mWeekList << ui->lblWeek0 << ui->lblWeek1 << ui->lblWeek2 << ui->lblWeek3 << ui->lblWeek4 << ui->lblWeek5;
    mDateList << ui->lblDate0 << ui->lblDate1 << ui->lblDate2 << ui->lblDate3 << ui->lblDate4 << ui->lblDate5;

    // 天气和天气图标
    mTypeList << ui->lblType0 << ui->lblType1 << ui->lblType2 << ui->lblType3 << ui->lblType4 << ui->lblType5;
    mTypeIconList << ui->lblTypeIcon0 << ui->lblTypeIcon1 << ui->lblTypeIcon2 << ui->lblTypeIcon3 << ui->lblTypeIcon4 << ui->lblTypeIcon5;


    // 天气指数
    mAqiList << ui->lblQuality0 << ui->lblQuality1 << ui->lblQuality2 << ui->lblQuality3 << ui->lblQuality4 << ui->lblQuality5;

    // 风向和风力
    mFxList << ui->lblFx0 << ui->lblFx1 << ui->lblFx2 << ui->lblFx3 << ui->lblFx4 << ui->lblFx5;
    mFlList << ui->lblFl0 << ui->lblFl1 << ui->lblFl2 << ui->lblFl3 << ui->lblFl4 << ui->lblFl5;
    weatherIcon.insert("暴雪", ":/res/type/BaoXue.png");
    weatherIcon.insert("暴雨", ":/res/type/BaoYu.png");
    weatherIcon.insert("暴雨到大暴雨", ":/res/type/BaoYuDaoDaBaoYu.png");
    weatherIcon.insert("大暴雨", ":/res/type/DaBaoYu.png");
    weatherIcon.insert("大暴雨到特大暴雨", ":/res/type/DaBaoYuDaoTeDaBaoYu.png");
    weatherIcon.insert("大到暴雪", ":/res/type/DaDaoBaoXue.png");
    weatherIcon.insert("大雪", ":/res/type/DaXue.png");
    weatherIcon.insert("大雨", ":/res/type/DaYu.png");
    weatherIcon.insert("冻雨", ":/res/type/DongYu.png");
    weatherIcon.insert("多云", ":/res/type/DuoYun.png");
    weatherIcon.insert("浮沉", ":/res/type/FuChen.png");
    weatherIcon.insert("雷阵雨", ":/res/type/LeiZhenYu.png");
    weatherIcon.insert("雷阵雨伴有冰雹", ":/res/type/LeiZhenYuBanYouBingBao.png");
    weatherIcon.insert("霾", ":/res/type/Mai.png");
    weatherIcon.insert("强沙尘暴", ":/res/type/QiangShaChenBao.png");
    weatherIcon.insert("晴", ":/res/type/Qing.png");
    weatherIcon.insert("沙尘暴", ":/res/type/ShaChenBao.png");
    weatherIcon.insert("特大暴雨", ":/res/type/TeDaBaoYu.png");
    weatherIcon.insert("undefined", ":/res/type/undefined.png");
    weatherIcon.insert("雾", ":/res/type/Wu.png");
    weatherIcon.insert("小到中雪", ":/res/type/XiaoDaoZhongXue.png");
    weatherIcon.insert("小到中雨", ":/res/type/XiaoDaoZhongYu.png");
    weatherIcon.insert("小雪", ":/res/type/XiaoXue.png");
    weatherIcon.insert("小雨", ":/res/type/XiaoYu.png");
    weatherIcon.insert("雪", ":/res/type/Xue.png");
    weatherIcon.insert("扬沙", ":/res/type/YangSha.png");
    weatherIcon.insert("阴", ":/res/type/Yin.png");
    weatherIcon.insert("雨", ":/res/type/Yu.png");
    weatherIcon.insert("雨夹雪", ":/res/type/YuJiaXue.png");
    weatherIcon.insert("阵雪", ":/res/type/ZhenXue.png");
    weatherIcon.insert("阵雨", ":/res/type/ZhenYu.png");
    weatherIcon.insert("中到大雪", ":/res/type/ZhongDaoDaXue.png");
    weatherIcon.insert("中到大雨", ":/res/type/ZhongDaoDaYu.png");
    weatherIcon.insert("中雪", ":/res/type/ZhongXue.png");
    weatherIcon.insert("中雨", ":/res/type/ZhongYu.png");
}

//实现更新UI的函数
void UpdateUI::updateUI(const Today& mToday, const Day* mDay)
{
    // ui->lblDate->setText(QDateTime::fromString(mToday.date, "yyyMMdd").toString("yyyy/MM/dd") + " " + mDay[1].week);
    ui->lblDate->setText(QDateTime::fromString(mToday.date, "yyyyMMdd").toString("yyyy/MM/dd") + ' ' + mDay[1].week);
    ui->lblCity->setText(mToday.city);
    // 3. 更新今天
    ui->lblTemp->setText(QString::number(mToday.wendu) + "°");
    ui->lblType->setText(mToday.type);
    ui->lblLowHigh->setText(QString::number(mToday.low) + "~" + QString::number(mToday.high) + "°C");

    ui->lblGanMao->setText("感冒指数: " + mToday.ganmao);
    ui->lblWindFx->setText(mToday.fx);
    ui->lblWindFl->setText(mToday.fl);
    ui->lblPM25->setText(QString::number(mToday.pm25));
    ui->lblShiDu->setText(mToday.shidu);
    ui->lblQuality->setText(mToday.quality);
    // 3. 更新六天
    for (int i = 0; i < 6; i++) {
        // 3.1 更新日期和时间
        mWeekList[i]->setText("周" + mDay[i].week.right(1));
        ui->lblWeek0->setText("昨天");
        ui->lblWeek1->setText("今天");
        ui->lblWeek2->setText("明天");

        QStringList ymdList = mDay[i].date.split("-");
        mDateList[i]->setText(ymdList[1] + "/" + ymdList[2]);
        // 3.2 更新天气类型
        mTypeList[i]->setText(mDay[i].type);
        mTypeIconList[i]->setPixmap(weatherIcon[mDay[i].type]);
        // 3.3 更新空气质量
        if (mDay[i].aqi >= 0 && mDay[i].aqi <= 50) {
            mAqiList[i]->setStyleSheet("QLabel{background-color:rgb(121, 184, 0);}");
            mAqiList[i]->setText("优");
        }
        else if (mDay[i].aqi > 50 && mDay[i].aqi <= 100) {
            mAqiList[i]->setStyleSheet("QLabel{background-color:rgb(255, 181, 23);}");
            mAqiList[i]->setText("良");
        }
        else if (mDay[i].aqi > 100 && mDay[i].aqi <= 150) {
            mAqiList[i]->setStyleSheet("QLabel{background-color:rgb(235, 87, 91);}");
            mAqiList[i]->setText("轻度");
        }
        else if (mDay[i].aqi > 150 && mDay[i].aqi <= 200) {
            mAqiList[i]->setStyleSheet("QLabel{background-color:rgb(235, 17, 27);}");
            mAqiList[i]->setText("中度");
        }
        else if (mDay[i].aqi > 200 && mDay[i].aqi <= 250) {
            mAqiList[i]->setStyleSheet("QLabel{background-color:rgb(170, 0, 0);}");
            mAqiList[i]->setText("重度");
        }
        else {
            mAqiList[i]->setStyleSheet("QLabel{background-color:rgb(110, 0, 0);}");
            mAqiList[i]->setText("严重");
        }
        //* 3.4 更新风向和风力
        mFxList[i]->setText(mDay[i].fx);
        mFlList[i]->setText(mDay[i].fl);
    }

}
